Repository navigation
Conversation
Looped models (e.g. Nanbeige4.2) run shared-weight layers more than once per forward pass, and each pass writes its own cache layer: the attention module adds an offset argument to `self.layer_idx`. The backend attached one vLLM attention layer per module, so all passes shared one KV cache. Find the offset argument in the attention forward and, only for models that have one, each module's offsets with a one-token forward pass of a meta-device copy of the model. Create one vLLM attention layer per cache layer, selected per call by a forward pre-hook. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Luca Motz <luca.motz@icloud.com>
Contributor
|
Documentation preview: https://vllm--69836.org.readthedocs.build/en/69836/ |
Contributor
Author
|
fyi @hmellor |
lucamotz
marked this pull request as ready for review
November 25, 2026 07:43
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Looped models run their shared-weight layers more than once per forward pass, with a KV cache per pass: the attention module adds an offset argument to
self.layer_idxfor the cache layer it writes. The Transformers backend creates one vLLM attention layer per attention module, so all passes share one KV cache and the output is wrong from the second forward pass on, without an error. This PR creates one attention layer per cache layer and gives each call its own.This affects the native Nanbeige4.2 class proposed in huggingface/transformers#50608, which is not merged yet. Today's path, the Hub remote code with
trust_remote_code, is unaffected. No open PR or issue covers looped models in this backend.Claims
self.layer_idxtake the same path as before.Validation
On GB10 (bf16, Nanbeige4.2-3B), greedy output with the native class matches Transformers for 32/32 tokens on 3 prompts in eager, compiled + CUDA graph, prefix-cache and chunked-prefill runs, and GSM8K (first 200 questions) goes from 0.005 on main to 0.93 (0.91 with the Hub remote code).
test_looped_modelfails on main and passes with this PR; the rest oftest_backend.pygives the same results with and without it.The native class in these runs is my Nanbeige implementation, posted as suggestions in my review on huggingface/transformers#50608, not that PR's head
aa124f9044. Both pass the samecache_offsetargument to the attention.Reproducer and parity (GB10, Nanbeige4.2-3B, bf16)
Reproducer: this PR's
pytest tests/models/transformers/test_backend.py -k looped_modelwith main's backend and a Transformers that has my Nanbeige implementation. It stops at the layer count; the output divergence is in the table below.Runs, against Transformers native greedy (
generate, bf16) on the same GPU:Prompts: chat (47 tokens), code (20), and both with another chat turn (114). Transformers main
ab014e21c5with my Nanbeige implementation (models/nanbeige/identical to the suggestions in my review). huggingface/transformers#50608 ataa124f9044uses the same contract: its attention writes cache layerself.layer_idx + cache_offset, withcache_offset = loop_idx * num_layers. Not run yet with that head. vLLMa87e3a6554with #66840, with main's Transformers backend package (b321d66fc8; no backend changes on main since) or this PR's (e331b330d), plus three files from main that the package needs on that base:transformers_utils/dynamic_module.py,multimodal/utils.py, andcompilation/wrapper.pywith d2da27e. The table gives the first differing token (0-based) of 32.trust_remote_code(Hub code), compiled, main and this PRChat prompt, eager:
Max |Δ logprob| over the matched tokens with this PR: 0.153 (bf16). The compiled run also passes with
VLLM_GPU_SYNC_CHECK=error(32/32 identical). The attention layers are named after their cache layer:model.layers.0.self_attn.attn,model.layers.22.self_attn.attn, ...,model.layers.43.self_attn.attn.GSM8K (GB10, Nanbeige4.2-3B, bf16)
First 200 test questions of
openai/gsm8k(revision740312add8), zero-shotllm.chatwith "Please reason step by step, and put your final answer within \boxed{}.", greedy,max_tokens=3072, compiled + CUDA graphs; scored on the last\boxed{}(else the last number).max_tokense331b330d), native classtrust_remote_code(Hub code)The native and Hub-code runs agree on 190 of the 200 verdicts. Summary lines:
Tests
GB10, same environment as above.
The 9 failing tests are the same with and without this PR and fail for environment reasons: gated meta-llama repos (2), no
sentence_transformers(1), a vLLM core older than main (_mark_tower_model(auto_offload=...), MTP for multimodal models; 5), and a test subprocess that imports the unbuilt checkout (_C_stable_libtorch; 1). Multimodal models end to end with this PR: not run yet.Details
Decisions for maintainers:
self.layer_idx. The model's forward computes them.total_num_hidden_layers(my Nanbeige implementation, Inkling2-loop) gives the number of cache layers but not which module serves which, and Dory, HRM-Text and Add Nanbeige4.2-3B huggingface/transformers#50608 do not have it.test_looped_modelskips until vLLM's Transformers pin has the native Nanbeige class, so CI covers this from then on. Merge now, or with that bump.Design
self.layer_idxgives the call's cache layer. The name differs (cache_offsetin Dory, Inkling2-loop and both Nanbeige implementations,cycle_offsetin HRM-Text), and so does the layout. The backend finds the argument in the attention forward's source, and each module's offsets from the meta forward pass, with vLLM's attention interface, which builds no masks.ModuleDictkeyed by offset, and a forward pre-hook passes the call's layer to the attention interface inkwargs.Limitations
ValueErrorfor looped models. The PP ✅︎ in the Nanbeige row of the supported models table describes the Hub remote code path; I'll update the row with the pin bump that brings the native class.ModuleDict, so the loader drops them and the layers keep a scale of 1.0.last_loopmode) are not handled. No model the backend can serve uses one.Layer 0 dispatches attention from more than one module); with this PR the offset pass raises a broadcast error, because its gate multiplies the attention output before the reshape. Dory and Inkling2-loop: vLLM loads its own config classes for them (DoryConfig,Inkling2LoopMMConfig), and loading fails the same way on main and with this PR.Pull Request Checklist
I used vLLM's
/pr-checklistskill. (Mandatory for agents, optional for humans).AI assistance was used during the creation of this PR.
Design Fit: Minimizes impact on core components, reuses existing functionality, and justifies added complexity.
Testing and Validation: Validates the change and ensures any added tests are meaningful and reliable, with CI coverage or documented CI resource constraints and validation performed outside CI.
Code Quality and Style: Keeps code and comments clear and concise, and updates relevant documentation and examples.
Pull Request Contents: Includes a brief summary and relevant links, supports claims with evidence, explains root causes and implementation trade-offs, and follows the contributing guide.
I used Claude Code for parts of this implementation.
🤖 Generated with Claude Code